package com.yjzx.util;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

public class RedisConst {
    //临时数据缓存ttl
    public static final String TEMP_DATA = "x";
    public static final Long TEMP_DATA_TTL = 1L;
    public static final TimeUnit TEMP_DATA_TTL_UNIT = TimeUnit.HOURS;


    @Configuration
    @AutoConfigureAfter(RedisAutoConfiguration.class)
    public static class RedissonConfiguration {

        @Autowired(required = false)
        RedisProperties redisProperties;

        @Bean
        public RedissonClient redissonClient(){
            Config config = new Config();
            config.useSingleServer()
                    .setAddress("redis://"+redisProperties.getHost()+":"+redisProperties.getPort())
                    .setPassword(redisProperties.getPassword());
            //1、创建redisson客户端
            RedissonClient redisson = Redisson.create(config);
            return redisson;
        }

    }
}
